package com.yesorno.activity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextPaint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yesorno.utils.ActivityUtils;
import com.yesorno.utils.HttpUtils;

/**
 * 拍照大厅的菜单
 * 
 * @author samoin
 * @version 0.1
 * @since 2011-08-08
 * 
 */
public class PhotoMenuActivity extends Activity {

	private RelativeLayout photo_menu_camera;
	private RelativeLayout photo_menu_photolist;

	// private long currentTopicId = 0;
	// private long currentUserId = 0;
	private String desc;

	/* 拍照的照片存储位置 */
	private static final File PHOTO_DIR = new File(ActivityUtils.getSDPath()
			+ "/DCIM/Camera");

	private File mCurrentPhotoFile;// 照相机拍照得到的图片

	/* 用来标识请求照相功能的activity */
	private static final int CAMERA_WITH_DATA = 3023;

	/* 用来标识请求gallery的activity */
	private static final int PHOTO_PICKED_WITH_DATA = 3021;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		String key = "publish_topic_img";
		Intent intent = new Intent();
		switch (requestCode) {
		case CAMERA_WITH_DATA:
			// 拍照后的回传
			try {
				Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
				HttpUtils.putBitmapToCache(key, cameraBitmap);
				Toast.makeText(PhotoMenuActivity.this, key, 2000);
			} catch (Exception e) {
			}
			intent.putExtra(key, key);
			intent.putExtra("desc", desc);
			PhotoMenuActivity.this.setResult(RESULT_OK, intent);
			PhotoMenuActivity.this.finish();
			break;
		case PHOTO_PICKED_WITH_DATA:
			// 相册的回传

			try {
				Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
				HttpUtils.putBitmapToCache(key, cameraBitmap);
			} catch (Exception e) {
			}

			intent.putExtra(key, key);
			intent.putExtra("desc", desc);
			PhotoMenuActivity.this.setResult(RESULT_OK, intent);
			PhotoMenuActivity.this.finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 拍照获取图片
	 * 
	 */
	protected void doTakePhoto() {
		try {
			// Launch camera to take photo for selected contact
			PHOTO_DIR.mkdirs();// 创建照片的存储目录
			mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名
			Intent intent = getTakePickIntent(mCurrentPhotoFile);
			startActivityForResult(intent, CAMERA_WITH_DATA);
		} catch (ActivityNotFoundException e) {
			Toast.makeText(this, R.string.photo_menu_photopickernotfoundtext,
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * 用当前时间给取得的图片命名
	 * 
	 */
	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
		String name = "yesorno_" + dateFormat.format(date) + ".jpg";
		return "camera.jpg";
	}

	public static Intent getTakePickIntent(File f) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo_menu);

		TextView hall_menu_title = (TextView) findViewById(R.id.photo_menu_title);
		TextPaint tp = hall_menu_title.getPaint();
		tp.setFakeBoldText(true);

		desc = getIntent().getStringExtra("desc");
		photo_menu_camera = (RelativeLayout) findViewById(R.id.photo_menu_camera);
		photo_menu_photolist = (RelativeLayout) findViewById(R.id.photo_menu_photolist);
		MyOnClickListener mocl = new MyOnClickListener();
		photo_menu_camera.setOnClickListener(mocl);
		photo_menu_photolist.setOnClickListener(mocl);
		// currentTopicId = intent.getLongExtra("currentTopicId", 0);
		// currentUserId = intent.getLongExtra("currentUserId", 0);
	}

	class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (v == photo_menu_camera) {
				doTakePhoto();
			}
			if (v == photo_menu_photolist) {
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				intent.putExtra("crop", "true");
				intent.putExtra("aspectX", 1);
				intent.putExtra("aspectY", 1);
				intent.putExtra("outputX", 160);// 返回的bitmap的大小
				intent.putExtra("outputY", 160);// 返回的bitmap的大小
				intent.putExtra("return-data", true);
				startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
			}
		}

	}

}
